import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
(async () => {
  let client: Client | undefined = undefined;
  const url = "http://localhost:3000/mcp";
  const baseUrl = new URL(url);
  try {
    client = new Client({
      name: "streamable-http-client",
      version: "1.0.0",
    });
    const transport = new StreamableHTTPClientTransport(new URL(baseUrl));
    await client.connect(transport);
    console.log("Connected using Streamable HTTP transport");
    client
      .callTool({
        name: "hello",
        arguments: {
          content: "Hello world from mcp client",
        },
      })
      .then((response) => {
        console.log(response);
      });
  } catch (error) {
    // If that fails with a 4xx error, try the older SSE transport
    console.log(
      "Streamable HTTP connection failed, falling back to SSE transport"
    );
    client = new Client({
      name: "sse-client",
      version: "1.0.0",
    });
  }
})();
